<?

	include_once("cmsConfig.php");
	$tableWriter = new CmsTableWriter(CMS_FORMAT, $db);
	$formDescriptior = new CmsTableWriter("formdescriptor", $db);
	
	function cmsDeleteRecord($formId, $documentCode, &$db, $authInfo, $message) {
		$cmsWriter = new CmsWriter($formId, $documentCode, $null = null, &$authInfo, &$db);
		$message .= $cmsWriter->deleteRecord($documentCode, $message);
		return $message;
	}
	
	//Extract Variables from the request
	$authInfo = extractObjectWithKeyFromArray('authInfo', $_SESSION);
	$returnURLEncoded = extractStringWithKeyFromArray("returnURLEncoded", $_REQUEST);
	$cancelURLEncoded = extractStringWithKeyFromArray("cancelURLEncoded", $_REQUEST);
	
	$documentCode = extractNumberWithKeyFromArray("documentCode", $_REQUEST);
	$confirmDelete = extractNumberWithKeyFromArray("confirmDelete", $_REQUEST);
	
	//Set up a return url instead and a default to home
	if(isset($returnURLEncoded) && strlen($returnURLEncoded) > 0){
		$returnURL = urldecode($returnURLEncoded);
	}
	if(!isset($returnURLEncoded) || strlen($returnURLEncoded) == 0){
		$returnURL = CMS_HOME;
		$returnURLEncoded = urlencode($returnURL);
	}
	//$relatedmessage = "<br />";
	
	//set up a cancel url also..// this is like the return url.. but for if the user decides to cancel.
	//in reality is should default to where the request came from..
	//Set up a return url instead and a default to home
	if(isset($cancelURLEncoded) && strlen($cancelURLEncoded) > 0){
		$cancelURL = urldecode($cancelURLEncoded);
	}	
	if(!isset($cancelURLEncoded) || strlen($cancelURLEncoded) == 0) {
		$cancelURL = CMS_HOME;
		$cancelURLEncoded = urlencode($cancelURL);		
	}
	
	
	if ($documentCode) {
		if ($confirmDelete) {
			$message = "";
			$documentContent = $tableWriter->readItem($documentCode);
			$formDescription = $formDescriptior->readItem($documentContent->formid);
			$message .= cmsDeleteRecord($documentContent->formid, $documentCode, $db, $authInfo, $message);
			//try and delete the specified code..
			// to delete content, you must be able to add it..
			
			// return destinations .. not sure about the need for this???
			if (isset($_SESSION['newFileNo'])){
				$_SESSION['fileNo'] = $_SESSION['newFileNo'	];
			} else {
				//unset($_SESSION['fileNo'] );//go to the default page.	
				$newFileNo = 1;			
				$_SESSION['fileNo'] = $newFileNo;//this does not appear to set? odd
				$_SESSION['formid'] = $formDescription->formid;//where has the form id gone
			}
			
			dialoguePage(array(	DLG_MESSAGE=> 	$message,
						DLG_LEFT_TEXT=>	DLG_EMPTY,
						DLG_LEFT_LINK=>	CMS_HOME,
						DLG_RIGHT_TEXT=>	DLG_OK,
						DLG_RIGHT_LINK=>	$returnURL ));					
			

		} else {
			dialoguePage(array(	DLG_MESSAGE=> 	"Are you sure you want to delete item $documentCode?<br />This is forever!<br />Any related documents will also be deleted.",
						DLG_LEFT_TEXT=>	DLG_NO,
						DLG_LEFT_LINK=>	$cancelURL,
						DLG_RIGHT_TEXT=>	DLG_YES,
						DLG_RIGHT_LINK=>	"$_SERVER[PHP_SELF]?fileNo=4&confirmDelete=$documentCode&documentCode=$documentCode&returnURLEncoded=" . urlencode($returnURLEncoded) ));			
		}
	} else {
		dialoguePage(array(	DLG_MESSAGE=> 	"No document code specifed, there is nothing to delete",
					DLG_LEFT_TEXT=>	DLG_EMPTY,
					DLG_LEFT_LINK=>	CMS_HOME,
					DLG_RIGHT_TEXT=>	DLG_OK,
					DLG_RIGHT_LINK=>	$returnURL ));		
	}
	
	
?>